<template>
  <div class="ai-exercise">
    <!-- 头部 -->
    <div class="header d-flex align-items-center">
      <router-link to="/english" class="text-dark">
        <i class="bi bi-chevron-left fs-4"></i>
      </router-link>
      <h5 class="mb-0 mx-auto">智能出题</h5>
    </div>

    <!-- 选择题型 -->
    <div 
      class="exercise-type-card" 
      :class="{ selected: selectedType === 'reading' }"
      @click="selectExerciseType('reading')"
    >
      <div class="d-flex justify-content-between align-items-center">
        <div>
          <h5 class="mb-2">阅读理解</h5>
          <p class="text-muted mb-0">根据生词生成阅读文章和题目</p>
        </div>
        <i class="bi bi-book fs-3"></i>
      </div>
    </div>

    <div 
      class="exercise-type-card" 
      :class="{ selected: selectedType === 'translation' }"
      @click="selectExerciseType('translation')"
    >
      <div class="d-flex justify-content-between align-items-center">
        <div>
          <h5 class="mb-2">翻译</h5>
          <p class="text-muted mb-0">生成包含目标词汇的翻译题</p>
        </div>
        <i class="bi bi-translate fs-3"></i>
      </div>
    </div>

    <!-- 生成选项 -->
    <div class="options-section">
      <h6 class="mb-3">设置选项</h6>
      <div class="mb-3">
        <label class="form-label">生词数量</label>
        <select class="form-select" v-model="vocabCount">
          <option :value="10">10</option>
          <option :value="20">20</option>
          <option :value="50">50</option>
        </select>
      </div>
    </div>

    <!-- 生成按钮 -->
    <button 
      class="generate-btn" 
      :disabled="!selectedType"
      @click="generateExercise"
    >
      开始生成
    </button>

    <!-- 加载遮罩 -->
    <div class="loading-overlay" v-show="loading">
      <div class="loading-content">
        <div class="spinner-border text-primary spinner" role="status"></div>
        <div>正在生成中，请稍候...</div>
        <small class="text-muted">预计需要15秒</small>
      </div>
    </div>
  </div>
</template>

<script>
import http from '@/router/http'

export default {
  name: 'AIExerciseView',
  
  data() {
    return {
      selectedType: null,
      vocabCount: 10,
      loading: false
    }
  },

  methods: {
    selectExerciseType(type) {
      this.selectedType = type
    },

    // 获取随机生词
    getRandomVocabs(vocabList, count) {
      // 过滤掉已掌握的生词
      const unmastered = vocabList.filter(vocab => vocab.situation === '未掌握')
      
      // 如果生词数量不足，返回全部未掌握的生词
      if (unmastered.length <= count) {
        return unmastered.map(vocab => vocab.newWord)
      }
      
      // 随机选择指定数量的生词
      const shuffled = [...unmastered].sort(() => 0.5 - Math.random())
      return shuffled.slice(0, count).map(vocab => vocab.newWord)
    },

    async generateExercise() {
      this.loading = true
      
      try {
        if (this.selectedType === 'translation') {
          this.$router.push({
            path: '/english/ai-translation',
            query: { vocabCount: this.vocabCount }
          })
        } else {
          this.$router.push({
            path: '/english/ai-reading',
            query: { vocabCount: this.vocabCount }
          })
        }
      } catch (error) {
        console.error('生成练习失败:', error)
        alert(error.message || '生成练习失败，请重试')
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style scoped>
.ai-exercise {
  background-color: #f0f9f9;
  min-height: 100vh;
}

.header {
  padding: 15px;
  background: white;
  position: sticky;
  top: 0;
  z-index: 1000;
  box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}

.exercise-type-card {
  background: white;
  border-radius: 15px;
  padding: 20px;
  margin: 15px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.exercise-type-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.exercise-type-card.selected {
  border: 2px solid #40e0d0;
}

.options-section {
  background: white;
  border-radius: 15px;
  padding: 20px;
  margin: 15px;
}

.generate-btn {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: #40e0d0;
  color: white;
  padding: 12px 30px;
  border-radius: 25px;
  border: none;
  box-shadow: 0 4px 12px rgba(64, 224, 208, 0.3);
}

.generate-btn:disabled {
  background: #ccc;
}

.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255,255,255,0.9);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.loading-content {
  text-align: center;
}

.spinner {
  width: 50px;
  height: 50px;
  margin-bottom: 15px;
}
</style>